package com.cdtye.shuoh.service.product.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cdtye.shuoh.domain.product.entity.ProductServices;
import com.cdtye.shuoh.domain.product.vo.ProductServicesVo;
import com.cdtye.shuoh.mapper.product.ProductServicesMapper;
import com.cdtye.shuoh.service.product.ProductCommandsService;
import com.cdtye.shuoh.service.product.ProductPropertiesService;
import com.cdtye.shuoh.service.product.ProductServicesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductServicesServiceImpl extends ServiceImpl<ProductServicesMapper, ProductServices> implements ProductServicesService {

    @Autowired
    private ProductServicesMapper productServicesMapper;

    @Autowired
    private ProductPropertiesService productPropertiesService;

    @Autowired
    private ProductCommandsService productCommandsService;


    public int insertServices(ProductServices productServices){
        return this.baseMapper.insert(productServices);
    }

    public List<ProductServices> findAllByProductIdentificationId(String productIdentification){
        return productServicesMapper.findAllByProductIdentificationId(productIdentification);
    }

    @Override
    public ProductServices findOneByProductIdentificationAndServiceId(String productIdentification, String serviceId) {
        return productServicesMapper.findOneByProductIdentificationAndServiceId(productIdentification,serviceId);
    }

    public IPage<ProductServices> pageList(ProductServicesVo vo){
        return productServicesMapper.pageList(vo.page(),vo);
    }

    public List<ProductServices> list(ProductServicesVo vo){
        return productServicesMapper.list(vo);
    }

    @Override
    public int deleteByProductIdentification(String productIdentification) {
       if(null!=productIdentification){
           List<ProductServices> serviceList = productServicesMapper.findAllByProductIdentificationId(productIdentification);
           for (ProductServices productServices : serviceList) {
               productPropertiesService.deleteByServiceId(productServices.getId());
               productCommandsService.deleteByServiceId(productServices.getId());
           }
           return productServicesMapper.deleteByProductIdentification(productIdentification);
       }
        return 0;
    }
}
